package com.example.house

import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.common.view.view.BaseMvvmActivity
import com.example.house.adapter.ZuHuAdapter
import com.example.house.databinding.ActivityPeopleBinding
import com.example.house.entity.PeopleEntity
import com.example.house.utils.PinyinUtils
import com.example.house.vm.HouseViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/house/PeopleActivity")
class PeopleActivity : BaseMvvmActivity<ActivityPeopleBinding>() {
    private val peopleViewModel:HouseViewModel by viewModels()
    override fun initView() {
        peopleViewModel.getPeople()
        peopleViewModel.mPeopleSuccess.observe(this){
            val beanBean = mutableListOf<PeopleEntity>()
            //按照企业名称拼音字母分组
            val map = it.groupBy {
                val pinYin = PinyinUtils.getPinyin(it.name)
                PinyinUtils.getFirstLetter(pinYin)
            }
            //对分组的首字母进行排序
            val list = map.keys.sorted()
            for (key in list){
                map[key]?.let {
                    beanBean.add(PeopleEntity(key,it))
                }
            }
            mDataBinding.rv.adapter = ZuHuAdapter(beanBean)
                .apply {
                    notifyDataSetChanged()
                }
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_people
}